home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / voxelspc / v9.c < prev    next >
C/C++ Source or Header  |  1996-03-30  |  23KB  |  992 lines

  1. /*
  2. #include <disclaimer.h>
  3.  
  4. There's supposed to be a real long passage here, but I removed it since most
  5. of you prolly dun need to read it. :)
  6.  
  7. Yeah yeah yeah, my same old saying applies again :
  8. Go on and rip this and be the tortoise of the decade.
  9.  
  10. Apologies to those 'Braveheart's out there who always have to read the above
  11. line in all my works, but there're simply too many 'DickHead's out there.
  12. Nevermind if you dun dare to flame them, I can do the favor for you :)
  13.  
  14. It's been a small journey, and a milestone, WaterLogic is 2 yrs old, happy
  15. birthday to ourselves...
  16.  
  17. Fast 'nuh nuhs' to:
  18. Mode XiX    : at least we know who the real lamer is now. See ya on IRC and
  19.             : we can continue the joke someday :)
  20. MysTiCal    : didn't I tell you?? didn't I tell you??
  21.             : if u dun understand the above sentence, ignore it :)
  22.             : how's ur leg? back to usual?
  23. BlackMagic  : Yes, we'll create a comotion, Cheers to our deal :)
  24. $uperLamers : yeah, we RULE and they SUXX! :)
  25. PowerSurge  : i still can't get over your intro, it should've been in TS95 :)
  26.             : where have you been?
  27. #iguana     : u guys should come to Singapore someday, I'll show you around
  28.             : see u the usual time ok?
  29. SDF         : keep working on your wormholes :)
  30. Zane        : Attitude part #2
  31.             : what's synonymous to NeVeR? I know how you hate it, but life's
  32.             : like that right? At least I know you'll make great music for
  33.             : the demo...hey thanx!
  34. Ng PeiSin   : I thought you wanted a MTV for Mystery Hugs?
  35. Morbid      : see ya at the other side of the fire lake
  36.             : so u joined ArchAngels huh?
  37. EricScorpios: remember to lend me your flame-proof jacket someday :)
  38. TeckShiong  : thanx for being my HQ, just one more d/l to DevSite! part 2 :)
  39. NickChan    : I'm still waiting for your board to open...
  40. ArchAngels  : cool white clothes...who picked the name anyway?
  41. Renegade    : *yawn* 
  42. Constellation : Hey I see no activity!
  43. AngPetChean : Get a nick and bring a crowd ok?
  44. Pandemonium : dun worry, I'll be your permanent supplier of socks. have fun
  45.             : btw, can you -finally- find your vertex normals??
  46. Scavenger   : thanx very much for the offer, but i really can't take it now.
  47. Trixter     : i'll tell u how to do it alright! quick send it!
  48.             : ok, now you know how to do motionblur in less than
  49.             : 1ins/pixel, you owe me a nice postcard :)
  50. Jmagic      : hey remember the credits ok? :)
  51.             : show me ur new engine when it's done!
  52.             : btw, 3x3 matrix is not enough, coz I realised this
  53.             : Cd=(1-d1/d2)*f/n where 'Cd' is diameter of circle of confusion,
  54.             : 'd1' is object distance, 'd2' is image distance, 'f' is
  55.             : the focal length. So 'Cd' dictates the size of the matrix :(
  56. PitBull     : why r u always buying new PCs?? I'll get a P-180 to beat u!
  57. Sandman     : next time I go to London, make sure u r around! :)
  58. Onyx        : pmode rules :)
  59. ....plus many I forgot....
  60.  
  61. The magazines who has my demo in their cd-roms, you guys owe me a few copies
  62. of your magazines! Dun forget!
  63.  
  64. Last words : fi er'uoy doog, s'ereht on deen ot garb
  65.  
  66. Where do I find this 'Rex Deathstar'??
  67. Try this:
  68. Email : deathstr@singnet.com.sg
  69. BBS   : Developer's Site BBS 561-0237
  70. IRC   : #coders #waterlogic (DeathScar/DeathStar)
  71. Telepathy is reserved for my close friends :)
  72.  
  73. Note : Fan mail will be appreciated, but not answered
  74.        Foreign distro site is welcomed
  75.        Requests to explain my sources will be ignored
  76.        Coding discussion is welcomed
  77.        Requests to distro on cd-roms need to send 2 copies to me
  78.        Questions on 'which book is good etc.' will be ignored
  79.        Flames will be used to fry my beef patties
  80.  
  81.                       WaterLogic productions
  82. ===========================================================================
  83. ............................Year 1994......................................
  84. 1.  REXINTRO.ZIP....debut selftro, very poorly done.
  85.  
  86. 2.  ASYLUM!.ZIP ....Advert for Asylum BBS (closed down now :( )
  87.  
  88. 3.  ICHIBAN!.ZIP....Advert for IchiBan BBS
  89.  
  90. 4.  ANARCHY!.ZIP....Advert for Anarchy Online BBS
  91.  
  92. 5.  ICHIBAN2.ZIP....2nd Advert for IchiBan BBS
  93.  
  94. 6.  COROM.ZIP   ....Advert for COROM PRODUCTIONS BBS
  95.  
  96. ...........................Year 1995.......................................
  97. 7.  _FACES.ZIP  ....Slideshow demo featuring realtime crossfading
  98.  
  99. 8.  TINIFIRE.ZIP....76 byte fire routine, real small huh? (Full sources)
  100.  
  101. 9.  TINYSTAR.ZIP....123 byte 3D starfield, another small one. (Full sources)
  102.  
  103. 10. DELAYDOT.ZIP....3D object morph with delaydots. (Full sources)
  104.  
  105. 11. PARTICLE.ZIP....3D Lissajous figures morph. (Full sources)
  106.  
  107. 12. PURENESS.ZIP....1st place megademo during 'The Scene 95' demo party at
  108.                     Seaview Hotel/Singapore on 2nd July 1995.
  109.  
  110. 13. COROMSRC.ZIP....Source codes to a BBS advert
  111.  
  112. 14. WATERFAL.ZIP....Source codes to a waterfall effect as seen in REXINTRO.
  113.  
  114. ............................Year 1996......................................
  115. 15. PLASWARP.ZIP....Source codes to the plasma and image warp effect as
  116.                     seen in Pureness (needs PURENESS.DAT to run)
  117.  
  118. 16. DEVSITE!.ZIP....BBS advert for Developer's Site BBS, now WaterLogic's
  119.                     HQ. Features SVGA 3D motion blur.
  120.  
  121. 16. AGEN-ART.ZIP....High-res JPGs of Agen's art seen in Pureness
  122.  
  123. 17. STARGATE.ZIP....Source codes to the texture-mapped wormhole seen
  124.                     in Pureness.
  125.  
  126. 18. MODELIST.ZIP....Generic VESA graphic mode lister with sources in C
  127.  
  128. 19. VOXELSPC.ZIP....Source codes to a Voxel landscape with color and height-
  129.                     field interpolation
  130.  
  131. 20. DEVSITE2.ZIP....Part 2 of the 'infamous' DevSite! BBS HQ advert
  132.                     (coming soon to a BBS near you)
  133.  
  134. 21. D???????.ZIP....Megademo for 'The Scene 96'!
  135.                     (Sense and Sensuality, needs ice to run)
  136. */
  137.  
  138. // VOXEL-SPACE SYSTEM BY  -=REX DEATHSTAR=-     March 1995
  139. // Version V9 : Further optimizations
  140.  
  141. #pragma inline
  142. asm .486p
  143.  
  144.  
  145. #include <math.h>
  146. #include <process.h>
  147. #include <dos.h>
  148. #include <stdio.h>
  149.  
  150.  
  151. void    graphics();
  152. void    text();
  153. void    vretrace();
  154. void    load_voxeldata();
  155. void    rotate_frame();
  156. void    load_heightfield();
  157. void    draw_scape2();
  158. void    perspective();
  159. void    bankpitch();
  160. void    smooth_mesh();
  161. void    smooth_cmap();
  162. void    move_light_source();
  163. void    shade_grid();
  164. void    gouraud_shade();
  165. void    post_process();
  166.  
  167. #define        rad    0.01745329
  168. #define        MAX_YAW        720
  169. #define        YAW_RES        360.0/MAX_YAW*rad
  170.  
  171. #define        NPOLYROWS    40
  172.  
  173. //#define    DYNAMIC 1
  174. //#define    REAL_LIGHT 1
  175. #define dToR(d) ((d)*(M_PI/180))
  176. #define pixelScale 63.0
  177.  
  178.     //----- GENERAL PURPOSE --------
  179.     char    palette[1024];
  180.     int    a,x,y,dummy;
  181.     unsigned int     idx,idx2,idx3;
  182.     long    nframes;
  183.     //-------------------------------
  184.  
  185.  
  186.     //---- VIEW POINT ORIENTATION -----
  187.     int    bank,bank_dir=0;
  188.     int    yaw,yaw_dir=1;
  189.     long    tx,ty;
  190.     long    tx_dir=260,ty_dir=256;
  191.  
  192.                     //      |
  193.     float    p1x=-40,p1y=NPOLYROWS;    //  1-----------2
  194.     float    p2x=40,p2y=NPOLYROWS;   //   \    |    /
  195.     float    p4x=-5,p3y=0;          //    \   |   /
  196.     float    p3x=5,p4y=0;           //_____4-----3_____
  197.     //----------------------------------      |
  198.                     //        ^origin
  199.     //------- GRID CONSTRUCTION --------
  200.     int    vcx,vcy,vcz;
  201.     long    sx,sy,fx,fy,sx2,sy2,fx2,fy2;
  202.     long    ix,iy;
  203.     long    esx,esy;
  204.     long    efx,efy,err1,err2;
  205.     int    slope;
  206.     //----------------------------------
  207.  
  208.  
  209.     //------ G-SHADED POLYGON ---------------------------------
  210.     int    xx1,yy1,yy3,y11,y12,y13,y21,y22,y23,y31,y32,y33;
  211.     char    color1,color2,color3,color4;
  212. unsigned int    eyy1,eyy2,fyy1,fyy2,temp,b;
  213.     int    ecolorx1,ecolorx2,ecolory,fcolorx1,fcolorx2,fcolory;
  214.     //---------------------------------------------------------
  215.  
  216.  
  217.     //------- GRID CORNER LOOK-UP TABLE --------
  218.     long    sine,cosine;
  219.     long    x1,y1,x2,y2,x3,y3,x4,y4; //coords of 4 corners of trapezium
  220.     //-------------------------------------------
  221.  
  222.  
  223.     //------- REAL-TIME G-SHADING -----------
  224.     long    Lx,Ly,Lz;
  225.     float    azimuth=dToR(0),azimuth_dir=0.05;
  226.     float    elevation=dToR(30),elevation_dir=0;
  227.  
  228.     long    Nx,Ny,Nz,Nz2,NzLz,NdotL;
  229.     int    brightness;
  230.     //---------------------------------------
  231.  
  232.  
  233.  
  234. //-------------- SEGMENT DATA --------------------------
  235. unsigned int    voxeldata,colormap,voxel,vbuffer;
  236. unsigned int    y_offset[204],jmptable[200];
  237.     char    cmap[NPOLYROWS*81],color;
  238. //------------------------------------------------------
  239.  
  240. asm    voxelfile    db    'height.dat',0
  241. asm    colorfile    db    'color.dat',0
  242. asm    palettefile    db    'voxelspc.pal',0
  243.  
  244. //---------------------------------------------------------------------------
  245. void main()
  246. {
  247.     if(allocmem(1024,&voxel)!=-1) exit(1);
  248.     if(allocmem(4096,&voxeldata)!=-1) exit(1);
  249.     if(allocmem(4096,&vbuffer)!=-1) exit(1);
  250.     if(allocmem(4096,&colormap)!=-1) exit(1);
  251.  
  252.     asm mov [dummy],offset one_vert_strip
  253.     for(a=0; a<200; a++)
  254.     jmptable[a]=dummy+(200*6)-(a*6);
  255.  
  256.     graphics();
  257.     load_voxeldata();
  258.     post_process();
  259.  
  260.  
  261.     {
  262.     MAIN_LOOP:;
  263.         rotate_frame();        //rotate 4 corners of landscape
  264.         load_heightfield();    //read elevation data from voxeldata
  265.         perspective();
  266.         draw_scape2();        //draw to screen
  267.  
  268.         nframes++;
  269.     }
  270.  
  271.     asm in al,60h
  272.     asm cmp al,1
  273.     asm jne MAIN_LOOP
  274.  
  275.  
  276.     text();
  277.         printf("No.of frames drawn:%d",nframes);
  278.  
  279. }
  280. //------------------
  281. void    perspective()
  282. {
  283.         asm    mov    es,voxel
  284.         asm    xor    di,di
  285.  
  286.     asm    mov    si,280        //altitude of view
  287.     asm    mov    ax,es:[2* ((NPOLYROWS-1)*81+40) ]
  288.     asm    sub    si,ax
  289.     asm    sub    si,128
  290.  
  291.         #define    PERSPECTIV 1000
  292.         // ax = bp*(height+elevation)/(z+256)
  293.  
  294.         asm    mov    slope,-NPOLYROWS/2
  295.         asm    mov    bp,600
  296.  
  297.         asm    mov    bx,PERSPECTIV
  298.         asm    add    bx,256        // bx => z in equation
  299.         outer_loop:
  300.         asm    mov    cx,81
  301.         do_perspective:
  302.         asm    mov    ax,es:[di]    //...'y', height info
  303.         asm    add    ax,si
  304.         asm    imul    bp        // y' = -y*d / (z+d)
  305.         asm    idiv    bx
  306.         asm    mov    es:[di],ax
  307.         asm    add    di,2
  308.         asm    dec    cx
  309.         asm    jnz    do_perspective
  310.  
  311.         asm    sub    bx,(PERSPECTIV/NPOLYROWS)
  312.         asm    cmp    bx,256
  313.         asm    jg    outer_loop
  314.  
  315.  
  316.       //bankpitch();
  317.       #ifdef REAL_LIGHT
  318.       shade_grid();
  319.       #endif
  320.  
  321.       smooth_mesh();
  322.       smooth_cmap();
  323. }
  324. //-------------------------------------------------------------------------
  325. void    load_heightfield()
  326. {
  327. //move an area of shape trapezius containing height data into voxel buffer
  328.     asm    mov    gs,[colormap]
  329.     asm    mov    fs,[voxeldata]
  330.     asm    mov    es,[voxel]
  331.  
  332.     //THE FOLLOWING DEFINES THE 4 CORNERS OF THE TRAPEZIUM
  333.     sx=x1; sy=y1;    //start point        //top-left
  334.     fx=x2;  fy=y2;    //end point        //top-rite
  335.     sx2=x4; sy2=y4;                //bot-left
  336.     fx2=x3;  fy2=y3;            //bot_rite
  337.     //-----------------------------------------------------
  338.  
  339. //FIND ERROR TERM FOR 2 DIAGONAL SIDES OF THE TRAPEZIUM
  340. esx=(sx2-sx)/NPOLYROWS;
  341. esy=(sy2-sy)/NPOLYROWS;
  342. efx=(fx2-fx)/NPOLYROWS;
  343. efy=(fy2-fy)/NPOLYROWS;        //to be donein NPOLYROWS steps
  344. //-----------------------------------------------------
  345.  
  346.  
  347.     //vcx,vcz are voxel x,z co-ord counters
  348.     //x,z used becoz of orientation of grid plane
  349.  
  350.     asm    xor    di,di    //voxel[] offset counter
  351.     asm    xor    si,si    //cmap[] offset counter
  352.  
  353.     idx=0;
  354.  
  355.  
  356. for(vcz=0; vcz<NPOLYROWS; vcz++)// define y start-end of grid
  357. {
  358.     ix=sx;
  359.     iy=sy;
  360.  
  361.     //err1=(fx-sx)/80; //error term for current
  362.     //err2=(fy-sy)/80; //scanline IN trapezium
  363.     asm    mov    esi,81
  364.     asm    mov    eax,dword ptr [fx]
  365.     asm    sub    eax,dword ptr [sx]
  366.     asm    cdq
  367.     asm    idiv    esi
  368.     asm    mov    dword ptr err1,eax
  369.     asm        mov    eax,dword ptr fy
  370.     asm        sub    eax,dword ptr sy
  371.     asm        cdq
  372.     asm        idiv    esi
  373.     asm        mov    dword ptr err2,eax
  374.  
  375.  
  376.     //define x start-end of grid, eg 0..320, step=4
  377.     asm    mov    cx,81
  378.     vcx_loop:
  379.         asm    mov    ebx,dword ptr iy
  380.         //asm    shr    ebx,8    // remove fix-point
  381.         //asm    shl    ebx,8    // mul by 256, map width in bytes
  382.         asm    xor    bl,bl
  383.  
  384.         asm    mov    eax,dword ptr ix
  385.         asm    shr    eax,8    // remove fix-point
  386.         asm    add    ebx,eax    //offset in voxeldata and colormap
  387.  
  388.  
  389.         asm    xor    ax,ax
  390.         asm    mov    al,fs:[bx]
  391.         asm    stosw        //save height info
  392.  
  393.         asm    mov    si,idx
  394.         asm    mov    al,gs:[bx]
  395.         asm    mov    cmap[si],al
  396.         asm    inc    idx
  397.  
  398.         ix+=err1;    //update pointer of scanline
  399.         iy+=err2;
  400.  
  401.     asm    dec    cx
  402.     asm    jnz    vcx_loop
  403.  
  404.     sx+=esx;    //update start point
  405.     sy+=esy;
  406.     fx+=efx;    //update end point
  407.     fy+=efy;
  408.  
  409. }
  410.  
  411. }
  412. //-------------------------------------------------------------------------
  413. void    draw_scape2()
  414. {
  415.     asm mov es,[voxel]
  416.     asm mov fs,[voxel]
  417.     asm mov di,offset 81*(NPOLYROWS-2)*2
  418.     asm mov ax,200
  419.     asm mov cx,81*2
  420.     asm rep stosw
  421.  
  422.     asm    xor    si,si
  423.     asm    mov    cx,NPOLYROWS*81
  424.     clip_loop:
  425.     asm    mov    ax,fs:[si]
  426.     asm    cmp    ax,200
  427.     asm    jl    less
  428.     asm    mov    ax,200
  429.     asm    jmp    done_clip
  430.     less:
  431.     asm    cmp    ax,0
  432.     asm    jg    more
  433.     asm    xor    ax,ax
  434.     more:
  435.     done_clip:
  436.     asm    mov    fs:[si],ax
  437.     asm    add    si,2
  438.     asm    dec    cx
  439.     asm    jnz    clip_loop
  440.  
  441.  
  442.  
  443.     //CLEAR VIRTUAL BUFFER
  444.     asm    mov    es,[vbuffer]
  445.     asm    xor    eax,eax
  446.     asm    xor    di,di
  447.     asm    mov    cx,200*320/4
  448.     asm    rep    stosd
  449.  
  450.  
  451.  
  452.     asm    mov    si,81*5*2
  453.     //asm    xor    si,si
  454.     asm    mov    bx,offset cmap+(81*5)
  455.  
  456.     //DRAW THE WHOLE VOXEL
  457.     asm    mov    cx,NPOLYROWS-6    //1st 5 lines invisible due
  458.     outer_loop:            //to perspective
  459.     asm    bswap    ecx
  460.     asm    xor    cx,cx
  461.  
  462.     inner_loop:
  463.  
  464.     //fyy1=yy1*256;        //top start at left
  465.     //fyy2=yy3*256;        //bot start at left
  466.     //eyy1=256*(yy2-yy1)/4;    //error term for top side of poly
  467.     //eyy2=256*(yy4-yy3)/4;    //error term for bot side of poly
  468.     asm    mov    eax,fs:[si]    //get y coord
  469.     asm    mov    dx,ax        //yy1
  470.     asm    mov    [fyy1+1],ax    //fyy1=ax<<8
  471.     asm    shr    eax,16        //yy2
  472.     asm    sub    ax,dx
  473.     asm    shl    ax,8-2
  474.     asm    mov    [eyy1],ax
  475.  
  476.     asm    mov    eax,fs:[si+81*2]
  477.     asm    mov    dx,ax        //yy3
  478.     asm    mov    [fyy2+1],ax    //fyy2=ax<<8
  479.     asm    shr    eax,16        //yy4
  480.     asm    sub    ax,dx
  481.     asm    shl    ax,8-2
  482.     asm    mov    [eyy2],ax
  483.  
  484.     asm    mov    [xx1],cx
  485.  
  486.     asm    push    si
  487.     asm    push    ecx
  488.     asm    push    bx
  489.  
  490.     //DON'T DESTROY BX B4 THIS!
  491.     //fcolorx1=color1<<8;
  492.     //fcolorx2=color3<<8;
  493.     asm    mov    cx,[bx]      // cx=color1,2
  494.     asm    mov    dx,[bx+81]   // dx=color3,4
  495.     asm    xor    ax,ax         //    1----2 <- colors 1,2,3,4
  496.     asm    mov    ah,cl        //    |    |
  497.     asm    mov    si,ax         //    3----4
  498.     asm    mov    ah,dl         // si=fcolorx1,bp=fcolorx2 are used to
  499.     asm    mov    bp,ax         // compute color err.term in y-dir
  500.  
  501.     //ecolorx1=256*(color2-color1)/4;    //color err.term in x-dir
  502.     //ecolorx2=256*(color4-color3)/4;    //across the polygon
  503.     asm    sub    ch,cl
  504.     asm    sub    dh,dl
  505.     asm     sar    cx,2
  506.     asm    sar    dx,2
  507.     asm    mov    [ecolorx1],cx
  508.     asm    mov    [ecolorx2],dx
  509.  
  510. //|||||||||||||||||||||||||||||||||||||||||||
  511. //||| regs used: si=fcolorx1, bp=fcolorx2 |||
  512. //|||||||||||||||||||||||||||||||||||||||||||
  513. asm    mov [a],4
  514. do_one_polygon:
  515.  
  516.     //compute color error term in y-direction for current vert.strip
  517.     //b=(fyy2-fyy1)/128;
  518.     asm    mov cx,[fyy2]
  519.     asm    sub cx,[fyy1]     // get length of current strip
  520.     asm    jns visible     // if pos+, current strip visible
  521.     asm    jmp stripskipper // else hidden, so skip
  522.     visible:
  523.     asm    shr    cx,8
  524.     asm    jz    b_is_zero    // if cx=0, avoid int0
  525.     asm    mov    ax,bp        // ax=fcolorx2-fcolorx1
  526.     asm    sub    ax,si
  527.     asm    cwd
  528.     asm    idiv    cx        // ax = ecolory = dx:ax/cx
  529.     b_is_zero:
  530.  
  531.     // ax,cx must be preserved from here on
  532.     asm    xor    bx,bx
  533.     asm    mov    bl,byte ptr [fyy1+1] // start at top of current strip
  534.     asm    add    bx,bx        // word array,after removing fix-pt
  535.     asm    mov    dx,320        // screen width in bytes
  536.     asm    mov    di,y_offset[bx]
  537.     asm    add    di,[xx1]    // start offset
  538.     asm    mov    bx,cx        // bx=length of current strip
  539.     asm    mov    cx,si         // cx=fcolorx1
  540.  
  541.     //|||| DO ONE VERTICAL STRIP ||||
  542.     asm    push     ds
  543.     asm    add    bx,bx
  544.     asm    mov    bx,jmptable[bx]
  545.     asm    mov     ds,[vbuffer]
  546.     asm    jmp    bx
  547.     asm    one_vert_strip:     // fill one vertical strip
  548.     asm    rept    201     // always draw one extra pixel
  549.     asm    mov    [di],ch     // draw pixel
  550.     asm    add    di,dx     // next scanline
  551.     asm    add    cx,ax     // fcolory+=ecolory
  552.     asm    endm
  553.     asm    pop    ds
  554.     //||||||||||||||||||||||||||||||||
  555.  
  556.     stripskipper:
  557.     xx1++;        //move on to next vertical strip
  558.     fyy1+=eyy1;    //update top side of polygon
  559.     fyy2+=eyy2;    //update bot side of polygon
  560.     asm add si,[ecolorx1]    //fcolorx1+=ecolorx1, color at top of strip
  561.     asm add bp,[ecolorx2]    //fcolorx2+=ecolorx2, color at bottom of strip
  562.  
  563. asm dec [a]
  564. asm jz  done_one_polygon
  565. asm jmp do_one_polygon
  566.  
  567. done_one_polygon:
  568.     asm    pop    bx
  569.     asm    pop    ecx
  570.     asm    pop    si
  571.  
  572.  
  573.     skipper:
  574.     asm    add    si,2
  575.     asm    inc    bx
  576.     asm    add    cx,4
  577.     asm    cmp    cx,320
  578.     asm    jge    next_poly_row
  579.     asm    jmp    inner_loop
  580.  
  581.     next_poly_row:
  582.     asm    bswap    ecx
  583.     asm    add    si,2
  584.     asm    inc    bx
  585.     asm    dec    cx
  586.     asm    jz    draw_done
  587.     asm    jmp    outer_loop
  588.  
  589.  
  590. draw_done:
  591.  
  592.  
  593.     //copy to VRAM
  594.     vretrace();
  595.     asm    mov    ax,0a000h
  596.     asm    mov    es,ax
  597.     asm     xor    di,di
  598.     asm    xor    si,si
  599.     asm    push    ds
  600.     asm    mov    ds,vbuffer
  601.     asm    mov    cx,200*320/4
  602.     asm    rep    movsd
  603.     asm    pop    ds
  604. }
  605.  
  606. //------------------------------------------
  607. void    shade_grid()
  608. {
  609.     Nz = (6 * 64) / 6;
  610.     Nz2 = Nz * Nz;
  611.     NzLz = Nz * Lz;
  612.  
  613.     asm    mov    fs,voxel
  614.     asm    mov    si,81*2
  615.     asm    xor    si,si
  616.     asm    mov    idx,0
  617.  
  618.  
  619.     //DRAW THE WHOLE VOXEL
  620.     asm    mov    cx,81*NPOLYROWS-(81)
  621.     shadeit:
  622.  
  623.     asm    mov    ax,fs:[si-2-81*2]
  624.     asm    mov    y11,ax
  625.     asm        mov    ax,fs:[si-81*2]
  626.     asm        mov    y12,ax
  627.     asm    mov    ax,fs:[si+2-81*2]
  628.     asm    mov    y13,ax
  629.  
  630.     asm    mov    ax,fs:[si-2]
  631.     asm    mov    y21,ax
  632.     asm        mov    ax,fs:[si]
  633.     asm        mov    y22,ax
  634.     asm    mov    ax,fs:[si+2]
  635.     asm    mov    y23,ax
  636.  
  637.     asm    mov    ax,fs:[si-2+81*2]
  638.     asm    mov    y31,ax
  639.     asm        mov    ax,fs:[si+81*2]
  640.     asm        mov    y32,ax
  641.     asm    mov    ax,fs:[si+2+81*2]
  642.     asm    mov    y33,ax
  643.  
  644.     asm    push    si
  645.     asm    push    cx
  646.     gouraud_shade();
  647.     asm    pop    cx
  648.     asm    pop    si
  649.  
  650.  
  651.     skipper:
  652.     asm    add    si,2
  653.     asm    inc    idx
  654.     asm    dec    cx
  655.     asm    jz    shade_done
  656.     asm    jmp    shadeit
  657.  
  658.  
  659.  
  660.     shade_done:;
  661.     move_light_source();
  662.  
  663. }
  664. //-------------------------------------------
  665. void    gouraud_shade()
  666. {
  667.  
  668.     Nx = (int)(y11 + y21 + y31 - y13 - y23 - y33);
  669.     Ny = (int)(y31 + y32 + y33 - y11 - y12 - y13);
  670.  
  671.         /* shade with distant light source */
  672.         if ( (NdotL = Nx*Lx + Ny*Ly + NzLz) < 0 )
  673.         brightness = 0;
  674.         else
  675.         brightness = NdotL / sqrt(Nx*Nx + Ny*Ny + Nz2);
  676.  
  677.     cmap[idx]=brightness;
  678. }
  679.  
  680.  
  681.  
  682.  
  683. //------------------------------
  684. void    move_light_source()
  685. {
  686.     Lx = cos(azimuth) * cos(elevation) * pixelScale;
  687.     Ly = sin(azimuth) * cos(elevation) * pixelScale;
  688.     Lz = sin(elevation) * pixelScale;
  689.  
  690.     azimuth+=azimuth_dir;
  691.     elevation+=elevation_dir;
  692.  
  693. }
  694. //----------------------------
  695. void    smooth_mesh()
  696. {
  697.       asm mov es,voxel
  698.       asm xor di,di
  699.  
  700.       asm mov dx,NPOLYROWS
  701.       outer:
  702.       asm mov cx,80
  703.       meow:
  704.       asm mov ax,es:[di]
  705.       asm add ax,es:[di+2]
  706.       asm shr ax,1
  707.       asm mov es:[di],ax
  708.       asm add di,2
  709.       asm dec cx
  710.       asm jnz meow
  711.  
  712.       asm add di,2
  713.  
  714.       asm dec dx
  715.       asm jnz outer
  716. }
  717. //-----------------------
  718. void    smooth_cmap()
  719. {
  720.     asm {
  721.     mov cx,NPOLYROWS*81
  722.     mov di,offset cmap+81
  723.     xor bx,bx
  724.     }
  725. interpolate_cmap:
  726.     asm {
  727.     xor ax,ax
  728.     mov al,[di]
  729.     mov bl,[di-1]
  730.     add ax,bx
  731.     mov bl,[di+1]
  732.     add ax,bx
  733.     mov bl,[di+81]
  734.     add ax,bx
  735.  
  736.     mov bl,[di+82]
  737.     add ax,bx
  738.     mov bl,[di+80]
  739.     add ax,bx
  740.     mov bl,[di-81]
  741.     add ax,bx
  742.     mov bl,[di-80]
  743.     add ax,bx
  744.  
  745.     shr ax,3
  746.     mov [di],al
  747.     inc di
  748.     dec cx
  749.     jnz interpolate_cmap
  750.     }
  751.  
  752. }
  753.  
  754. //------------------------------------------------------------------------
  755. void    rotate_frame()
  756. {
  757. x1 = 256*( p1x*cos(YAW_RES*yaw) + p1y*sin(YAW_RES*yaw) )+tx;
  758. y1 = 256*( p1y*cos(YAW_RES*yaw) - p1x*sin(YAW_RES*yaw) )+ty;
  759.  
  760. x2 = 256*( p2x*cos(YAW_RES*yaw) + p2y*sin(YAW_RES*yaw) )+tx;
  761. y2 = 256*( p2y*cos(YAW_RES*yaw) - p2x*sin(YAW_RES*yaw) )+ty;
  762.  
  763. x3 = 256*( p3x*cos(YAW_RES*yaw) + p3y*sin(YAW_RES*yaw) )+tx;
  764. y3 = 256*( p3y*cos(YAW_RES*yaw) - p3x*sin(YAW_RES*yaw) )+ty;
  765.  
  766. x4 = 256*( p4x*cos(YAW_RES*yaw) + p4y*sin(YAW_RES*yaw) )+tx;
  767. y4 = 256*( p4y*cos(YAW_RES*yaw) - p4x*sin(YAW_RES*yaw) )+ty;
  768.  
  769.     yaw+=yaw_dir;
  770.     if(yaw<0) yaw=MAX_YAW;
  771.     else
  772.     if(yaw>MAX_YAW) yaw=0;
  773.  
  774.  
  775.     #ifndef DYNAMIC
  776.     tx+=tx_dir;
  777.     ty+=ty_dir;
  778.     #else
  779.     tx=300*sin(0.7*tx_dir*rad);    tx_dir++;
  780.     ty=128*cos(ty_dir*rad);        ty_dir++;
  781.     #endif
  782.  
  783.  
  784.  
  785. }
  786.  
  787.  
  788.  
  789. //------------------------------------------------------------------------
  790. void    load_voxeldata()
  791. {
  792.     //LOAD VOXEL DATA
  793.     asm    mov    ax,3d00h
  794.     asm    mov    dx,offset voxelfile
  795.     asm    int    21h
  796.     asm    mov    dummy,ax
  797.  
  798.     asm    mov    bp,2    //no.of 32Kb segments
  799.     asm    xor    dx,dx
  800.     load_loop:
  801.     asm    mov    bx,dummy    //load 32Kb
  802.     asm    mov    cx,0x8000
  803.     asm    mov    ah,3fh
  804.     asm    push    ds
  805.     asm    mov    ds,voxeldata
  806.     asm    int    21h
  807.     asm    pop    ds
  808.     asm    add    dx,0x8000
  809.     asm    dec    bp
  810.     asm    jnz    load_loop
  811.  
  812.     asm    mov    bx,dummy
  813.     asm    mov    ah,3eh
  814.     asm    int    21h
  815.  
  816.     //LOAD COLOR DATA
  817.     asm    mov    ax,3d00h
  818.     asm    mov    dx,offset colorfile
  819.     asm    int    21h
  820.     asm    mov    dummy,ax
  821.  
  822.     asm    mov    bp,2    //no.of 32Kb segments
  823.     asm    xor    dx,dx
  824.     load_loop2:
  825.     asm    mov    bx,dummy    //load 32Kb
  826.     asm    mov    cx,0x8000
  827.     asm    mov    ah,3fh
  828.     asm    push    ds
  829.     asm    mov    ds,[colormap]
  830.     asm    int    21h
  831.     asm    pop    ds
  832.     asm    add    dx,0x8000
  833.     asm    dec    bp
  834.     asm    jnz    load_loop2
  835.  
  836.     asm    mov    bx,dummy
  837.     asm    mov    ah,3eh
  838.     asm    int    21h
  839.  
  840.  
  841.     asm    mov    ax,0x3d00
  842.     asm    mov    dx,offset palettefile
  843.     asm    int    0x21
  844.     asm    mov    bx,ax
  845.     asm    mov    cx,768
  846.     asm    mov    ah,0x3f
  847.     asm    lea    dx,[palette]
  848.     asm    int    0x21
  849.     asm    mov    bx,dummy
  850.     asm    mov    ah,0x3e
  851.     asm    int    0x21
  852.  
  853.     asm mov dx,0x3c8
  854.     asm xor ax,ax
  855.     asm out dx,al
  856.     asm inc dx
  857.     asm mov cx,768
  858.     asm lea si,[palette]
  859.     asm cld
  860.     asm rep outsb
  861.  
  862.  
  863. }
  864. //------------------------------------------
  865. void    graphics()
  866. {
  867.     for(a=0; a<204; a++)
  868.     y_offset[a]=a*320;
  869.  
  870.     asm    mov    ax,13h
  871.     asm    int    10h
  872. }
  873. //-----------------
  874. void    text()
  875. {
  876.     asm    mov    ax,3
  877.     asm    int    10h
  878. }
  879. //------------------
  880. void    vretrace()
  881. {
  882.     asm    mov    dx,3dah
  883.     vr1:
  884.     asm    in    al,dx
  885.     asm    test    al,8
  886.     asm    jz    vr1
  887.     vr2:
  888.     asm    in    al,dx
  889.     asm    test    al,8
  890.     asm    jnz    vr2
  891. }
  892. //---------------------
  893. void    bankpitch()
  894. {
  895.     //DO BANKING : Z-axis rotation
  896.     sine=1024*sin(bank*rad);
  897.     cosine=1024*cos(bank*rad);
  898.     asm    mov    es,voxel
  899.     asm    xor    di,di
  900.     asm    mov    cx,NPOLYROWS
  901.     outer_loop:
  902.     asm    mov    edx,-160
  903.  
  904. loop3D_Z:
  905.     asm    mov    ebx,edx
  906.     asm    imul    ebx,dword ptr sine
  907.  
  908.     asm    movsx    eax,word ptr es:[di]
  909.     asm    imul    eax,dword ptr cosine
  910.     asm    sub    eax,ebx
  911.     asm    shr    eax,10
  912.     asm    stosw
  913.  
  914.     asm    add    edx,4
  915.     asm    cmp    edx,160
  916.     asm    jle    loop3D_Z
  917.  
  918.     asm    dec    cx
  919.     asm    jnz    outer_loop
  920.  
  921.  
  922.     bank+=bank_dir;
  923.     if(bank<=-15 || bank>=15) bank_dir*=-1;
  924.  
  925. }
  926. //-------------------
  927. void    post_process()
  928. {
  929.     asm    mov es,voxeldata
  930.     asm    xor di,di
  931.     asm    mov bx,5
  932.     smoothen:
  933.     asm    xor ax,ax
  934.     asm    mov al,es:[di]
  935.     asm    add al,es:[di-1]
  936.     asm    adc ah,0
  937.     asm    add al,es:[di+1]
  938.     asm    adc ah,0
  939.     asm    add al,es:[di-256]
  940.     asm    adc ah,0
  941.     asm    add al,es:[di+256]
  942.     asm    adc ah,0
  943.  
  944.     asm    div bl
  945.     asm    mov es:[di],al
  946.     asm    inc di
  947.     asm    jnz smoothen
  948.  
  949.  
  950.     /*move_light_source();
  951.  
  952.     Nz = (6 * pixelScale) / 6;
  953.     Nz2 = Nz * Nz;
  954.     NzLz = Nz * Lz;
  955.  
  956.     asm mov idx,0
  957.     asm mov es,colormap
  958.     asm mov fs,voxeldata
  959.     asm xor si,si
  960.     process_loop:
  961.     asm xor ax,ax
  962.     asm    mov    al,fs:[si-256-1]
  963.     asm    mov    y11,ax
  964.     asm    mov    al,fs:[si-256]
  965.     asm    mov    y12,ax
  966.     asm    mov    al,fs:[si-256+1]
  967.     asm    mov    y13,ax
  968.  
  969.     asm    mov    al,fs:[si-1]
  970.     asm    mov    y21,ax
  971.     asm    mov    al,fs:[si]
  972.     asm    mov    y22,ax
  973.     asm    mov    al,fs:[si+1]
  974.     asm    mov    y23,ax
  975.  
  976.     asm    mov    al,fs:[si+256-1]
  977.     asm    mov    y31,ax
  978.     asm    mov    al,fs:[si+256]
  979.     asm    mov    y32,ax
  980.     asm    mov    al,fs:[si+256+1]
  981.     asm    mov    y33,ax
  982.  
  983.     asm    pusha
  984.     gouraud_shade();
  985.     asm    popa
  986.  
  987.     asm    mov    al,cmap[0]
  988.     asm    mov    es:[si],al
  989.     asm    inc    si
  990.     asm    jnz    process_loop*/
  991. }
  992.